<?php

require '../utils/conn.php';
require_once 'ApnsPHP/Autoload.php';
function pushMessageToAllUser($message){

	$query = "SELECT token,member_id FROM t_token as t where not exists (select 1 from t_token where token = t.token and member_id<t.member_id);";
	$result = mysql_query($query);
	// Instanciate a new ApnsPHP_Push object
	$push = new ApnsPHP_Push(
	ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION,
	'apns_pro.pem'
	);

	// Set the Root Certificate Autority to verify the Apple remote peer
//	$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');

	// Connect to the Apple Push Notification Service
	$push->connect();

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$deviceToken = $row['token'];
		$memberId = $row['member_id'];
		$countquery = "SELECT * FROM t_member WHERE member_id = ".$memberId;
		$newsResult = mysql_query($countquery);
		$countRow = mysql_fetch_array($newsResult, MYSQL_ASSOC);
		$badge = $countRow['news_count'];
		$apnsMessage = new ApnsPHP_Message($deviceToken);
		$apnsMessage->setText($message);
		$apnsMessage->setSound();
		$apnsMessage->setBadge((int)$badge);
		$push->add($apnsMessage);
	}
	echo "<br/>";
	// Send all messages in the message queue
	$push->send();

	// Disconnect from the Apple Push Notification Service
	$push->disconnect();

	// Examine the error message container
	$aErrorQueue = $push->getErrors();
	if (!empty($aErrorQueue)) {
		var_dump($aErrorQueue);
	}

}

function pushMessageToAllUser1($message) {
	$query = "SELECT token,member_id FROM t_token as t where not exists (select 1 from t_token where token = t.token and member_id<t.member_id);";
	$result = mysql_query($query);

	$mode = "development";

	$ctx = stream_context_create();
	$fp;
	if($mode == "development")
	{
		stream_context_set_option($ctx, "ssl", "local_cert", "apns_dev.pem");
		$fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
	}
	else
	{
		stream_context_set_option($ctx, "ssl", "local_cert", "apns_pro.pem");
		$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
	}

	stream_context_set_option($ctx, "ssl", "passphrase", "123123");
	if (!$fp) {
		return;
	}
	ob_flush();
	flush();
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$deviceToken = $row['token'];
		$memberId = $row['member_id'];
		$countquery = "SELECT * FROM t_member WHERE member_id = ".$memberId;
		$newsResult = mysql_query($countquery);
		$countRow = mysql_fetch_array($newsResult, MYSQL_ASSOC);
		$badge = $countRow['news_count'];
		echo "pushNotification(".$deviceToken." | ".$message." | ".$badge.")<br/>";
		$body = array("aps" => array("alert" => $message, "badge" => (int)$badge, "sound"=>'default'));
		$payload = json_encode($body);
		$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
		fwrite($fp, $msg, strlen($msg));
		if (!$result)
		echo 'Message not delivered' . PHP_EOL;
		else
		echo 'Message successfully delivered' . PHP_EOL;
		ob_flush();
		flush();
	}

	fclose($fp);
}



function pushNotification($deviceToken, $message, $badge) {
	$mode = "product";

	$ctx = stream_context_create();
	$fp;
	if($mode == "development")
	{
		stream_context_set_option($ctx, "ssl", "local_cert", "apns_dev.pem");
		$fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
	}
	else
	{
		stream_context_set_option($ctx, "ssl", "local_cert", "apns_pro.pem");
		$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
	}

	stream_context_set_option($ctx, "ssl", "passphrase", "123123");
	if (!$fp) {
		return;
	}

	$body = array("aps" => array("alert" => $message, "badge" => (int)$badge, "sound"=>'default'));
	$payload = json_encode($body);
	$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
	fwrite($fp, $msg);
	fclose($fp);
}

?>

